//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.TridentWordAddIn.Common;
using Microsoft.TridentWordAddIn.ResourceHelper;

namespace Microsoft.TridentWordAddIn.Presentation
{
    /// <summary>
    /// The starting point of the GenePattern package plug-in. This class should be
    /// instatiated by the Infusion.ScientificAddIn component
    /// </summary>
    public class TridentPackage : ITridentPackage, IUserControlCreator
    {
        private const string ViewAssociation = "btnViewAssociation";
        private const string DashBoard = "btnDashboard";
        private const string ViewConnection = "btnViewConnections";
        private const string ConnectionsDropDown = "dropdownConnections";
        private const string RunWorkflow = "btnRunWorkflow";
        private const string AssociateWorkflow = "btnAssociateWorkflow";
        private const string InsertTridentLink = "btnInsertTridentLink";
        private const string RerunWorkflow = "btnRerunWorkflow";
        private const string WizardFreeCheckBox = "chkWizardFree";
        private const string ProtectDocument = "btnProtectDocument";
        private readonly IController controller;

        /// <summary>
        /// Initializes a new instance of the <see cref="TridentPackage"/> class.
        /// </summary>
        public TridentPackage()
        {
            this.controller = Controller.Instance(this);
        }

        #region ITridentPackage Members

        /// <summary>
        /// Gets a ribbon extension from the package.
        /// </summary>
        /// <returns>
        /// an object that should be of type Office.IRibbonExtensibility
        /// </returns>
        public object GetRibbon()
        {
            return new TridentRibbon(this.controller);
        }

        /// <summary>
        /// Sets the task pane manager for this package. This controls
        /// all the task panes associated with the package
        /// </summary>
        /// <param name="taskPaneManager">The task pane manager.</param>
        public void SetTaskPaneManager(ITaskPaneManager taskPaneManager)
        {
            this.controller.InitializeBaseTaskPaneManager(taskPaneManager);
        }

        /// <summary>
        /// Sets the package host.
        /// </summary>
        /// <param name="packageHost">The package host.</param>
        public void SetPackageHost(IThisAddInPackageHost packageHost)
        {
            this.controller.SetPackageHost(packageHost);
        }

        /// <summary>
        /// Sets the document storage.
        /// </summary>
        /// <param name="documentStorage">The document storage.</param>
        public void SetDocumentStorage(IDocumentStorage documentStorage)
        {            
            this.controller.DataStore = new DataStore(documentStorage);
        }

        #endregion

        #region IUserControlCreator Members

        /// <summary>
        /// Gets a user control matching the specified <c>TaskPaneKey</c>.
        /// </summary>
        /// <param name="taskPaneKey">The task pane key.</param>
        /// <returns>
        /// a <c>UserControl</c> is there is a match otherwise null
        /// </returns>
        public UserControl GetUserControl(TaskPaneKey taskPaneKey)
        {
            UserControl result = null;
            switch (taskPaneKey)
            {
                case TaskPaneKey.Connections:
                    ConnectionsTaskPane connectionsTaskPane = new ConnectionsTaskPane();
                    result = connectionsTaskPane;
                    break;
                case TaskPaneKey.Dashboard:
                    DashboardTaskPane dashboardTaskPane = new DashboardTaskPane();
                    result = dashboardTaskPane;
                    break;
                case TaskPaneKey.ViewAssociations:
                    ViewAssociatedWorkflowsTaskPane viewAssociatedWorkflowsTaskPane = new ViewAssociatedWorkflowsTaskPane();
                    result = viewAssociatedWorkflowsTaskPane;
                    break;
                case TaskPaneKey.InsertTridentLink:
                case TaskPaneKey.EditWorkflow:
                case TaskPaneKey.AssociateWorkflow:
                    AssociateWorkflowTaskPane associateWorkflowTaskPane = new AssociateWorkflowTaskPane();
                    associateWorkflowTaskPane.AssociationMode = taskPaneKey;
                    result = associateWorkflowTaskPane;
                    break;
                case TaskPaneKey.RunWorkflow:
                    RunWorkflowTaskPane runWorkflowTaskPane = new RunWorkflowTaskPane();
                    result = runWorkflowTaskPane;
                    break;
                case TaskPaneKey.RerunWorkflow:
                    RerunWorkflowTaskPane rerunWorkflowTaskPane = new RerunWorkflowTaskPane();
                    result = rerunWorkflowTaskPane;
                    break;
            }

            return result;
        }

        /// <summary>
        /// Gets the title matching the specified <c>TaskPaneKey</c>.
        /// </summary>
        /// <param name="taskPaneKey">The task pane key.</param>
        /// <returns>a <c>string</c> with the matching title</returns>
        public string GetTaskPaneTitle(TaskPaneKey taskPaneKey)
        {
            string titleKey = taskPaneKey.ToString() + "Title";
            string taskPaneTitle = ResourceHelper.ResourceFetcher.GetString(titleKey);
            if (string.IsNullOrEmpty(taskPaneTitle))
            {
                Trace.WriteLine("No title found for taskPaneKey: " + taskPaneKey.ToString(), TraceLevel.Error.ToString());
                taskPaneTitle = "Default title";
            }

            return taskPaneTitle;
        }

        /// <summary>
        /// Gets the task pane key.
        /// </summary>
        /// <param name="taskPaneTitleOrControlId">The task pane title.</param>
        /// <returns>TaskPaneKey</returns>
        public TaskPaneKey GetTaskPaneKey(string taskPaneTitleOrControlId)
        {
            TaskPaneKey result = TaskPaneKey.Default;

            if (taskPaneTitleOrControlId.MatchesAtLeastOne(ViewAssociation, ResourceFetcher.GetString("ViewAssociationsTitle")))
            {
                result = TaskPaneKey.ViewAssociations;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(DashBoard, ResourceFetcher.GetString("DashboardTitle")))
            {
                result = TaskPaneKey.Dashboard;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(ViewConnection, ResourceFetcher.GetString("ConnectionsTitle")))
            {
                result = TaskPaneKey.Connections;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(AssociateWorkflow, ResourceFetcher.GetString("AssociateWorkflowTitle")))
            {
                result = TaskPaneKey.AssociateWorkflow;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(InsertTridentLink, ResourceFetcher.GetString("InsertTridentLinkTitle")))
            {
                result = TaskPaneKey.InsertTridentLink;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(ResourceFetcher.GetString("EditWorkflowTitle")))
            {
                result = TaskPaneKey.EditWorkflow;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(RerunWorkflow, ResourceFetcher.GetString("RerunWorkflowTitle")))
            {
                result = TaskPaneKey.RerunWorkflow;
            }
            else if (taskPaneTitleOrControlId.MatchesAtLeastOne(RunWorkflow, ResourceFetcher.GetString("RunWorkflowTitle")))
            {
                result = TaskPaneKey.RunWorkflow;
            }
            else
            {
                Trace.WriteLine("No taskPaneKey found for title: " + taskPaneTitleOrControlId, TraceLevel.Error.ToString());
            }

            return result;
        }

        /// <summary>
        /// Gets the control id.
        /// </summary>
        /// <param name="taskPaneKey">The task pane key.</param>
        /// <returns>string</returns>
        public string GetRibbonControlId(TaskPaneKey taskPaneKey)
        {
            string result = string.Empty;
            switch (taskPaneKey)
            {
                case TaskPaneKey.ViewAssociations:
                    result = ViewAssociation;
                    break;
                case TaskPaneKey.Dashboard:
                    result = DashBoard;
                    break;
                case TaskPaneKey.Connections:
                    result = ViewConnection;
                    break;
                case TaskPaneKey.AssociateWorkflow:
                    result = AssociateWorkflow;
                    break;
                case TaskPaneKey.InsertTridentLink:
                    result = InsertTridentLink;
                    break;
                case TaskPaneKey.RerunWorkflow:
                    result = RerunWorkflow;
                    break;
                case TaskPaneKey.RunWorkflow:
                    result = RunWorkflow;
                    break;
                default:
                    Trace.WriteLine("No controlId for taskPaneKey: " + taskPaneKey.ToString(), TraceLevel.Error.ToString());
                    result = "Default Title";
                    break;
            }

            return result;
        }

        /// <summary>
        /// Gets the control id.
        /// </summary>
        /// <param name="controlKey">The control key.</param>
        /// <returns>string</returns>
        public string GetRibbonControlId(ControlKey controlKey)
        {
            string result = null;
            switch (controlKey)
            {
                case ControlKey.ConnectionDropDown:
                    result = ConnectionsDropDown;
                    break;
                case ControlKey.ViewAssociationButton:
                    result = ViewAssociation;
                    break;
                case ControlKey.DashboardButton:
                    result = DashBoard;
                    break;
                case ControlKey.ConnectionsButton:
                    result = ViewConnection;
                    break;
                case ControlKey.AssociateWorkflowButton:
                    result = AssociateWorkflow;
                    break;
                case ControlKey.InsertTridentLinkButton:
                    result = InsertTridentLink;
                    break;
                case ControlKey.RerunWorkflowButton:
                    result = RerunWorkflow;
                    break;
                case ControlKey.ProtectDocumentButton:
                    result = ProtectDocument;
                    break;
            }

            return result;
        }

        #endregion
    }
}
